package com.his.controller;

import com.his.commons.core.dto.PageInfo;
import com.his.commons.core.dto.Result;
import com.his.commons.core.exception.HisException;
import com.his.commons.web.exception.ParamException;
import com.his.commons.web.util.ValidatedUtil;
import com.his.controller.form.*;
import com.his.controller.info.LoginUserInfo;
import com.his.controller.info.UserByDayInfo;
import com.his.controller.info.UserByIdsInfo;
import com.his.controller.info.UserAllInfo;
import com.his.service.UserInfoService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 梅津铧
 * @since 2022-03-22
 */
@Api(tags = "员工接口")
@RestController
@RequestMapping("/userInfo")
@GlobalTransactional(rollbackFor = Exception.class)
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    /**
     * 员工登录
     * @param userJobNum  员工工号
     * @param userPassword  登录密码
     * @return  员工信息
     * @throws HisException
     */
    @ApiOperation("员工登录")
    @GetMapping("/loginUser")
    public Result<UserAllInfo> LoginUser(
            @RequestParam(value = "userJobNum",required = false) Integer userJobNum,
            @RequestParam(value = "userPassword",required = false) String userPassword
    ) throws HisException{

        UserAllInfo userAllInfo = userInfoService.loginUser(userJobNum,userPassword);
        if(null == userAllInfo) throw new HisException("该员工不存在，登陆失败",500);
        return Result.getInstance(UserAllInfo.class).setCode(200).setMsg("登录成功").setData(userAllInfo);
    }


    /**
     * 员工注册
     * @param newUserForm 新员工信息
     * @param bindingResult
     * @throws HisException
     */
    @ApiOperation("员工注册")
    @PostMapping("/inductionUser")
    public Result InductionUser(@RequestBody @Valid NewUserForm newUserForm, BindingResult bindingResult) throws HisException{

        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = userInfoService.increaseUser(newUserForm);
        if(!b) throw new HisException("注册员工失败",500);
        return Result.getInstance().setCode(200).setMsg("员工注册成功");
    }

    /**
     * 修改权限
     * @param newUserForm  员工的新信息
     * @param bindingResult
     * @throws HisException
     */
    @ApiOperation("修改权限")
    @PutMapping("/renewUser")
    public Result RenewUser(@RequestBody @Valid ModifyUserForm newUserForm,BindingResult bindingResult) throws HisException {
        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = userInfoService.modifyUser(newUserForm);
        if(!b) throw new HisException("修改权限失败",500);
        return Result.getInstance().setCode(200).setMsg("员工信息修改成功");
    }

    /**
     * 获取当前登录员工的信息
     * @return  当前登录员工的信息
     * @throws HisException
     */
    @ApiOperation("获取当前登录员工的信息")
    @GetMapping("/obtainUserinfo/{userJobNum}")
    public Result<LoginUserInfo> ObtainUserinfo(@PathVariable("userJobNum") Integer userJobNum) throws HisException {

        LoginUserInfo loginUserInfo = userInfoService.obtainUserInfo(userJobNum);
        if(null == loginUserInfo) throw new HisException("未获取到当前登录员工的信息",500);
        return Result.getInstance(LoginUserInfo.class).setCode(200).setMsg("成功获取登录用户的信息").setData(loginUserInfo);
    }

    /**
     * 根据工号/姓名获取员工信息
     * @param obtainInfo  员工工号/姓名信息
     * @return  查询到的员工信息
     * @throws HisException
     */
    @ApiOperation("根据工号/姓名获取员工信息")
    @GetMapping("/obtainUser")
    public Result<List<UserAllInfo>> ObtainUserByInfo(@RequestParam(value = "obtainInfo",required = false) String obtainInfo) throws HisException {

        List<UserAllInfo> userAllInfo = userInfoService.obtainByInfo(obtainInfo);
        if(null == userAllInfo) throw new HisException("未获取到该员工信息",500);
        return Result.getInstanceList(UserAllInfo.class).setCode(200).setMsg("成功获取用户的信息").setData(userAllInfo);
    }

    /**
     * 获取员工信息
     * @param pagenum 页码
     * @param pagesize  每页数据量
     * @return  某页的员工信息
     * @throws ParamException
     */
    @ApiOperation("获取员工信息")
    @GetMapping("/obtainUserByPage")
    public Result<PageInfo<UserAllInfo>> ObtainUserByPageForm(
            @RequestParam(value = "pagenum",required = false) Integer pagenum,
            @RequestParam(value = "pagesize",required = false) Integer pagesize) throws HisException
    {

        PageInfo<UserAllInfo> userPageInfo = userInfoService.obtainByPage(pagenum,pagesize);
        if(userPageInfo.getRecord().size()==0) throw new HisException("未获取到该页的员工信息",500);
        return Result.getInstancePageInfo(UserAllInfo.class).setData(userPageInfo);
    }

    /**
     * 根据科室、诊室获取员工信息
     * @param userDepartment  科室名称
     * @param userDisagnosisRoom  诊室名称
     * @return  员工信息
     * @throws ParamException
     */
    @ApiOperation("根据科室、诊室获取员工信息")
    @GetMapping("/obtainUserById")
    public Result<UserByIdsInfo> ObtainUserById(@RequestParam(value = "userDepartment",required = false) String userDepartment,
                                                @RequestParam(value = "userDisagnosisRoom",required = false) String userDisagnosisRoom) throws HisException {

        UserByIdsInfo userByIdsInfo = userInfoService.obtainByNames(userDepartment,userDisagnosisRoom);

        if(null == userByIdsInfo) throw new HisException("未获取到员工信息",500);
        return Result.getInstance(UserByIdsInfo.class).setCode(200).setMsg("查询员工信息成功").setData(userByIdsInfo);
    }

    /**
     * 注销员工
     * @param userJobNum  注销员工的工号
     * @throws HisException
     */
    @ApiOperation("注销员工")
    @DeleteMapping("/logoutUser/{userJobNum}")
    public Result LogoutUser(@PathVariable("userJobNum") Integer userJobNum) throws HisException {

        boolean b = userInfoService.logoutUser(userJobNum);
        if(!b) throw new HisException("注销失败",500);
        return Result.getInstance().setCode(200).setMsg("注销成功");
    }

    /**
     * 批量注销员工
     * @param userJobNums  注销的员工工号
     * @throws ParamException
     */
    @ApiOperation("批量注销员工")
    @DeleteMapping("/logoutSomeUser")
    public Result LogoutSomeUser(@RequestParam(value = "userJobNums",required = false) List<Integer> userJobNums) throws HisException {

        boolean b = userInfoService.logoutUsers(userJobNums);
        if(!b) throw new HisException("批量注销失败",500);
        return Result.getInstance().setCode(200).setMsg("注销成功");
    }

    /**
     * 批量获取员工信息
     * @param userJobNums  员工工号
     * @throws ParamException
     * @return 员工信息
     */
    @ApiOperation("批量获取员工信息")
    @GetMapping("/obtainSomeUser")
    public Result<List<UserByDayInfo>> ObtainSomeUser(@RequestParam(value = "userJobNums",required = false) List<Integer> userJobNums) throws HisException {

        List<UserByDayInfo> userByDayInfos = userInfoService.obtainUsers(userJobNums);
        if(userByDayInfos.size()<=0) throw new HisException("批量获取员工信息失败",500);
        return Result.getInstanceList(UserByDayInfo.class).setCode(200).setMsg("获取成功").setData(userByDayInfos);
    }

}

